require(['loading'], function(loading) {
	var selectTab='jyy';
	//获取url中的id参数
	var params = getUrlParams();
	if(params.type!=null && params.type == 2){
		selectTab='pl';
		console.log("pl");
	}else{
		console.log("jyy");
		selectTab='jyy';
	}
    var jyyOver=false;
    var jyypageNo=1;
    var level = 1;
    var jyySearch = true;
    //应用实例化
    var myAttention = new Vue({
        el: '#myAttention',
        data: {
			isjyy:selectTab=='jyy',
			ispl:selectTab=='pl',
            jyyList: [],
            plList:[],
            classData: [],
            showLoading: false,
            jyyUrl: wxUrl + '/v3.2/follow/traderlist',
            plUrl: wxUrl + '/v3.2/follow/classlist',
            classUrl: wxUrl + '/v3.2/goodsClass/findSonGoodsClass',
            followClassUrl: wxUrl + '/v3.2/follow/class',
            jyyitem: {
                params:{
                    token: token,
                    userToken: userToken,
                    pageNo:jyypageNo,
                    pageSize:10
                }
            },
            plitem: {
                params:{
                    token: token,
                    userToken: userToken
                }
            },
            classItem: {
                params:{
                    token: token,
                    parentId: ''
                }
            },
            followClassItem:{
                params:{
                   token: token,
                   userToken: userToken, 
                   classId:'',
                   action:1
                }
            }
        },
        ready: function() {
            bindClass();
            bindScroll();
            bindTabClick();
            this.getjyyList();
            this.getplList();
            bindItemSwipe();
            
        },
        methods: {
            getjyyList: function() {
                if(jyySearch){
                    jyySearch = false;
                }else{
                    return;
                }
                this.$http.get(this.jyyUrl, this.jyyitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data!=null){
                            var jyylist = result.data.list;
                            if(jyylist){
                                this.jyyList=this.jyyList.concat(jyylist);
                            }
                            if(result.data.pageNo>=result.data.totalPage){
                                jyyOver=true;
                            }
                            jyypageNo++;
                            this.jyyitem.params.pageNo=jyypageNo;
                        }
                        jyySearch = true;
                    },function(response) {
                        jyySearch = true;
                    });
            },
            getplList: function() {
                this.$http.get(this.plUrl, this.plitem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        if(result.data!=null){
                            var pllist = result.data.list;
                            this.$set('plList', pllist);
                        }
                    },function(response) {
                        //errorCallback
                    });
            },
            getClassData: function(e) {
                //获取顶级分类
                this.$http.get(this.classUrl, this.classItem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        var classData = result.data.result;
                        this.$set('classData', classData);
                        $("#classBox").scrollTop(0);
                    },function(response) {
                        
                    });
            },
            getChildClass: function(e){
                var dId = $(e.currentTarget).attr('id');
                this.classItem.params.parentId = dId;
                var topClass = $(e.currentTarget).text();
                if(typeof($(e.currentTarget).attr('data-id')) != "undefined"){
                    this.islowest = true; //判断已经是最底层
                }
                $("#classBox").scrollTop(0);
                //获取子类
                this.$http.get(this.classUrl, this.classItem)
                    .then(function(response) {
                        var result = JSON.parse(response.body);
                        var classData = result.data.result;
                        this.$set('classData', classData);
                        if(level == 1){
                            $("#className").text(topClass);
                        }
                        if(this.islowest){
                            $("#goodsClass").animate({"bottom": "-100%"});
                            this.islowest = false;
                            this.classItem.params.parentId = '';
                            this.followClassItem.params.classId = dId;
                            this.followClass();
                        }else{
                            level ++;
                        }
                    },function(response) {
                        
                    });
            },
            followClass: function(){
                this.$http.get(this.followClassUrl, this.followClassItem)
                    .then(function(response) {
                        this.getplList();
                    });
            }
        }
        
    });
    
    function bindTabClick(){
        //顶级切换
        $('.tab_1 li').click(function() {
            var siblings = $(this).siblings();
            var index = $(this).index();
            if(siblings.length > 0) {
                $(this).addClass('bg');
                siblings.removeClass("bg");
                $(".cont_1 .tab").eq(index).addClass('xian').siblings().removeClass("xian");
                selectTab=$(".cont_1 .tab").eq(index).attr("select-data");
            }
            $(".item").removeClass('selected');
        });
    }
	//左滑删除
    function bindItemSwipe(){
        //左滑删除
        $("#tc_lists").on("swipeleft",".item", function(){
            $(this).addClass('selected').siblings().removeClass('selected');
            $(this).find(".list_contents").css("padding-right","0")
                .parent(".item").siblings().children(".list_contents")
                .css("padding-right","0.15rem");
            var divHeight = $(this).find(".contents").height();
            $(this).find(".remove").css("height",divHeight);
            $(this).find("a.remove").on("click",function(){
                //这里需要获取id,删除
                var itemData =  $(this).parent().attr("item-data");
                $("#delete").show();
                $(".btn-success").click(function(){
                    var ajaxUrl = wxUrl+'/v3.2/follow/trader';
                    var ajaxData={token: token,userToken: userToken,action:0,traderId:itemData};
                    if(itemData){
                        $.ajax({
                          type: 'POST',
                          url: ajaxUrl,
                          data: ajaxData,
                          success: function(){
                            $("#delete").hide();
                            $(".item[item-data='"+itemData+"']").remove();
                          }
                        });
                    }
                });
                $(".btn-default").click(function () {
                    $("#delete").hide();
                    $(".item").removeClass('selected');
                    $(".list_contents").css("padding-right","0.15rem");
                });
            });
        }).on("swiperight", function(){
            $(".item").removeClass('selected');
            $(".list_contents").css("padding-right","0.15rem");
        });
    }

    function bindScroll(){
        window.onscroll = function(){
            $(".item").removeClass('selected');
            $(".list_contents").css("padding-right","0.15rem");
            if($(window).scrollTop() + $(window).height() == $(document).height()) {
                if(selectTab=='jyy'){
                    if(!jyyOver){
                        myAttention.getjyyList();
                    }
                }
            }
        }
    }
	$(function(){
		bindplDel();
	});
    function bindplDel(){
        $("#hd_lists").on("click",".fq_scpinlei", function(){
            var itemData = $(this).parent(".hd_list1").attr("item-data");
            var ajaxUrl = wxUrl+'/v3.2/follow/class';
            var ajaxData={token: token,userToken: userToken,action:0,classId:itemData};
            if(itemData){
                $.ajax({
                  type: 'POST',
                  url: ajaxUrl,
                  data: ajaxData,
                  success: function(){
                    myAttention.getplList();
                    $(this).parent(".hd_list1").remove();
//                  $(".hd_list1[item-data='"+itemData+"']").remove();
                  }
                });
            }
        });
    }

    function bindClass(){
        //打开分类选项
        $("#selectClass").click(function(){
            $("#goodsClass").animate({"bottom": 0});
            myAttention.getClassData();
        });
        //关闭分类选项
        $(".class_header").click(function(){
            $("#goodsClass").animate({"bottom": "-100%"});
        });
    }

});

// 为可控制返回指定页面所有添加pushState
history.pushState({}, document.title, null);
pushVHistory();
window.addEventListener("popstate", function() {
    // 跳转到订单列表页面
	goBackVHistory();
});